/** @file         stack.c
 *  @brief        简要说明
 *  @details      详细说明
 *  @author       lzm
 *  @date         2021-09-06 09:42:22
 *  @version      v1.0
 *  @copyright    Copyright By lizhuming, All Rights Reserved
 *  @blog         https://www.cnblogs.com/lizhuming/
 *
 **********************************************************
 *  @LOG 修改日志:
 **********************************************************
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 数组式
typedef int se_type; /* 元素类型 */
#define STACK_SIZE 100 /* 栈元素个数 */
typedef struct
{
    int top;                    /* 栈顶指针 */
    se_type data[STACK_SIZE];   /* 栈空间 */
}stack_array_t;

/**
 * @name   stack_array_creat
 * @brief  
 * @param  
 * @retval 
 * @author lzm
 */
stack_array_t *stack_array_creat(void)
{
    stack_array_t *stack_ptr = NULL;

    stack_ptr = (stack_array_t *)malloc(sizeof(stack_array_t));
    if(stack_ptr == NULL)
        return NULL;
    memset(stack_ptr, 0x00, sizeof(stack_array_t));

    stack_ptr->top = -1;

    return stack_ptr;
}

/**
 * @name   stack_array_destroy
 * @brief  
 * @param  
 * @retval 
 * @author lzm
 */
int stack_array_destroy(stack_array_t *stack)
{
    if(stack != NULL)
    {
        free(stack);
        return 0;
    }

    return -1;
}

/**
 * @name   stack_array_clear
 * @brief  
 * @param  
 * @retval 
 * @author lzm
 */
int stack_array_clear(stack_array_t *stack)
{
    if(stack == NULL)
        return -1;

    stack->top = -1;

    return 0;
}

/**
 * @name   stack_array_empty
 * @brief  
 * @param  
 * @retval 
 * @author lzm
 */
int stack_array_empty(stack_array_t *stack)
{
    if(stack == NULL)
        return -1;

    if(stack->top == -1)
        return 1;
    
    return 0;
}

/**
 * @name   stack_array_full
 * @brief  
 * @param  
 * @retval 
 * @author lzm
 */
int stack_array_full(stack_array_t *stack)
{
    if(stack == NULL)
        return -1;

    if(stack->top == STACK_SIZE-1)
        return 1;
    
    return 0;
}

/**
 * @name   stack_array_length
 * @brief  
 * @param  
 * @retval 
 * @author lzm
 */
int stack_array_length(stack_array_t *stack)
{
    if(stack == NULL)
        return -1;

    return stack->top + 1;
}

/**
 * @name   stack_array_push
 * @brief  
 * @param  
 * @retval 
 * @author lzm
 */
int stack_array_push(stack_array_t *stack, se_type elem)
{
    if(stack_array_full(stack) != 0)
        return -1;

    stack->top++;
    stack->data[stack->top] = elem;

    return 0;
}

/**
 * @name   stack_array_pop
 * @brief  
 * @param  
 * @retval 
 * @author lzm
 */
int stack_array_pop(stack_array_t *stack, se_type *elem)
{
    if(stack_array_empty(stack) != 0 || elem == NULL)
    {
        return -1;
    }

    *elem = stack->data[stack->top];
    stack->top--;

    return 0;
}

/**
 * @name   stack_array_get_top
 * @brief  
 * @param  
 * @retval 
 * @author lzm
 */
int stack_array_get_top(stack_array_t *stack, se_type *elem)
{
    if(stack_array_empty(stack) != 0 || elem == NULL || stack->top >= STACK_SIZE)
    {
        return -1;
    }

    *elem = stack->data[stack->top];

    return 0;
}